
// 写入日志
const restTool = require('../../middlewares/mysql/mysql');
const until = require('../../middlewares/until');

const methods = {

    /**
     * 添加错误信息
     */ 
    async uploadError (params) {
        const date = until.getCurrentDate();
        const sql = `INSERT INTO record (system_id, error_info, error_type, browser_id, project_type, add_time, update_time) VALUES (${params['systemId']},'${params['errorInfo']}','${params['errorType']}',${params['browserId']}, ${params['projectType']}, '${date}', '${date}')`;
        const result = await restTool.query(sql, params);
        console.log(result);
        return result;
    },

    /**
     * 获取错误列表
     */ 
    async getRecord (params) {
        /**
         * 判断分页情况
         */
        const limit = params['pageSize'] ? parseInt(params['pageSize']) : 10;
        const start = params['pageNo'] ? (parseInt(params['pageNo']) - 1) * limit : 0;

        let sqlFilter = '';
        // 系统类型
        if(params['systemId']){
            sqlFilter += ` ${sqlFilter?',':''} user_name='${params['systemId']}'`;
        }
        // 浏览器类型
        if(params['browserId']){
            sqlFilter += ` ${sqlFilter?',':''} browserId='${params['browserId']}'`;
        }
        // 错误id
        if(params['id']){
            sqlFilter += ` ${sqlFilter?',':''} id='${params['id']}'`;
        }
        // 错误信息
        if(params['errorInfo']){
            sqlFilter += ` ${sqlFilter?',':''} error_info LIKE '%${params['errorInfo']}%'`;
        }
        const sql = `SELECT *  FROM record ${sqlFilter? 'WHERE' + sqlFilter: ''} LIMIT ${start},${limit}`;
        const result = await restTool.query(sql, params);
        // let res;
        // if(result.length){
        //     res['rows'] = result;
        //     res['totalPage'] = result.length;
        // }
        return result;
    },

    /**
     * 获取错误详情
     * params: {errorId: 1}
     */ 
    async getRecordDetail (params) {

        const sql = `SELECT *  FROM record WHERE id= ${parseInt(params['errorId'])}`;
        const result = await restTool.query(sql, params);
        return result;
    }
}
const errorService = {
    async get(type, params) {
        try{
            res = await methods[type](params);
            return res;
        }catch(err){
            res = []
        }
        return res;
    }
}

module.exports = errorService;